package com.ihr360.oneclick.util;

import com.ihr360.commons.lang.DateUtils;
import com.ihr360.oneclick.exception.OneClickException;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by Eric on 2018/1/16.
 */
public class ExcelImportValidator {

    private static final String IDENTITY_CARD = "IDENTITY_CARD";

    /**
     * 校验手机号正则
     */
    public static final Pattern REGEX_MOBILE = Pattern.compile("^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

    private static final String CHINA_MOBILE = "+86";
    private static final String SINGAPORE_MOBILE = "+65";

    private static final int CHINA_MOBILE_LENGTH = 11;
    private static final int SINGAPORE_MOBILE_LENGTH = 8;


    /**
     * 邮箱校验
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 手机校验
     * @param mobileNumber
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 是否允许发布
     */
    public static final List<String> allowPublishList = new ArrayList<>(Arrays.asList("是", "否"));

    /**
     * 校验姓名
     *
     * @param staffName 姓名
     */
    public static void validateStaffName(String staffName) {
        if (StringUtils.isBlank(staffName)) {
            throw new OneClickException("one_click_term_name_is_null", "姓名输入为空！");
        }
    }

    /**
     * 手机号码校验
     *
     * @param mobileNo 手机号
     */
    public static void validateMobileNo(String mobileNo) {
        if (StringUtils.isBlank(mobileNo)) {
            throw new OneClickException("one_click_term_mobile_is_null", "手机号码输入为空！");
        }
        Boolean isValid = true;
        int mobileLength = mobileNo.length();
//        if (mobileNo.startsWith(CHINA_MOBILE)) {
//            String mobile = mobileNo.substring(CHINA_MOBILE.length(), mobileLength);
//            if (mobile.length() != CHINA_MOBILE_LENGTH) {
//                isValid = false;
//            }
//        } else if (mobileNo.startsWith(SINGAPORE_MOBILE)) {
//            String mobile = mobileNo.substring(SINGAPORE_MOBILE.length(), mobileLength);
//            if (mobile.length() != SINGAPORE_MOBILE_LENGTH) {
//                isValid = false;
//            }
//        } else {
//            if (mobileLength != CHINA_MOBILE_LENGTH) {
//                isValid = false;
//            }
//        }
        if (mobileLength != CHINA_MOBILE_LENGTH) {
            isValid = false;
        }
        if (!isValid) {
            throw new OneClickException("one_click_term_mobile_not_correct", "手机号码格式不正确！");
        }
    }
    
    /**
     * 校验身份证号
     *
     * @param idCardType 证件类型
     * @param idCardNo   证件号
     */
    public static void validateIdCardNo(String idCardType, String idCardNo) {
        if (StringUtils.isBlank(idCardNo)) {
            throw new OneClickException("one_click_term_id_card_no_is_null", "身份证号码输入为空！");
        }
        //校验身份证号
        if (IDENTITY_CARD.equals(idCardType) && !IdcardValidator.isValidatedAllIdcard(idCardNo)) {
            throw new OneClickException("one_click_term_id_card_no_is_not_legal", "身份证号码不合法！");
        }
    }

    /**
     * 校验所属月份
     *
     * @param month
     */
    public static void validateMonth(Object month) {
        if (month == null || StringUtils.isBlank(month.toString())) {
            throw new OneClickException("one_click_term_month_is_null", "所属月份输入为空！");
        }
        if (month instanceof Date) {
            return;
        }
        Date mon = DateUtils.parse(month.toString());
        if (mon == null) {
            throw new OneClickException("one_click_term_month_not_correct", "所属月份格式错误");
        }

    }

    /**
     * 校验入职日期
     *
     * @param entryDate
     */
    public static void validateEntryDate(Object entryDate) {
        if (entryDate == null || StringUtils.isBlank(entryDate.toString())) {
            throw new OneClickException("one_click_term_entry_date_is_null", "入职日期输入为空！");
        }
        if (entryDate instanceof Date) {
            return;
        }
        Date entry = DateUtils.parse(entryDate.toString());
        if (entry == null) {
            throw new OneClickException("one_click_term_entry_date_not_correct", "入职日期格式错误！");
        }
    }

    /**
     * 校验是否允许发布
     *
     * @param allowPublish 是否允许发布
     */
    public static void validateAllowPublish(String allowPublish) {
        if (StringUtils.isBlank(allowPublish)) {
            throw new OneClickException("one_click_term_allow_publish_is_null", "是否允许发布输入为空！");
        } else if (!allowPublishList.contains(allowPublish)) {
            throw new OneClickException("one_click_term_allow_publish_not_correct", "是否允许发布输入格式有误!");
        }
    }

}
